<?php
/**
 * Search a Mission
 *
 * @author Xavier VILAIN
 *
 */


use Fox\SalesDistribution\Sales;
use Fox\SearchEngine;
use Fox\Common\Customer;
use Fox\Html\Interval;
use Fox\Html\DropList;
use Fox\Html\Input;
use Fox\Html\Screen;
use Doctrine\DBAL\Connection;
use Fox\Generator;
use Fox\Html\AnchorObject;
use Fox\SalesDistribution\Sales\Inquiry\Status;
use Fox\SalesDistribution\Sales\Inquiry\Type;
use Fox\SalesDistribution\Sales\Partner\Role;
use Fox\SalesDistribution\Sales\Inquiry;
// Init
$ajax = Xview::instance()->val("ajax",false);

$output = null;
// HTML Components
$screen = new Screen("eSearchInquiry","vlook");
$screen->toolbar()->add(new Fox\Html\Button("add","?mod=inquiry&act=create",
						Msg(Sales::BUNDLE_NAME,Sales::LABEL_CREATE_INQUIRY)));
$block = new Fox\Html\Block("BLOOK","Offre");
// Filters
$block->add(new Input(array("propection","reference")))
		->useLike();
$block->add(new Fox\Html\Input("keyword",false));
$block->add(new Interval(array("emission_date","start_date")));
$screen->add($block);

if($screen->submitted() || isset($_POST['keyword']))
{
	$inputs = $screen->get();
	if(!$screen->errors())
	{
		// Customize search objects
		$searchedObject = new Inquiry();
		$idFieldName = array_keys($searchedObject->getIds());
		$idFieldName = $idFieldName[0];
		$orderBy = array("reference" => false);
		$finder = $searchedObject;
		$fragments[] = User::current()->inquiryFragments();
		$fragments[] = $block->item("start_date")->fragments();
		$fragments[] = $block->item("reference")->fragments();
		$joins = array(Inquiry::$JOIN_PARTNERS);
		try {
			$result = array();
			if(empty($inputs['keyword']))
			{
				$result = $finder->find(Fox\Dao\SqlFragment::combine($fragments),$orderBy,$joins,null,true);
			}
			else
			{
				$searchType[] = new Fox\Dao\SqlFragment("grouping_type = ?",array($searchedObject::className()));
				$searchEngine = new SearchEngine();
				$matches = $searchEngine->search(preg_split("/ /",strtolower($inputs['keyword']))
													,SearchEngine::MODE_LIKE,
													$searchType);
				if(is_array($matches))
				{
					foreach($matches as $match)
					{
						$groupingIds[] = $match['grouping_id'];
					}

					$fragments[] = new Fox\Dao\SqlFragment("t.$idFieldName IN (?)",
															array($groupingIds),
															array(Doctrine\DBAL\Connection::PARAM_STR_ARRAY));

					$resultTmp = $finder->find(Fox\Dao\SqlFragment::combine($fragments),$orderBy,$joins);
					foreach($resultTmp as $object)
					{
						$result[$object->getId()] = $object;
					}
					if(count($result) == 0)
						Fox\Message::Warning(Msg("SE",2));
				}
				else
				{
					Fox\Message::Warning(Msg("SE",2));
				}

			}
			//---- Output View	----
			$data = null;
			$grid = Xview::instance()->component("inquiry","result");
			$grid->title(Msg(Fox::MSGCL,143,count($result)));

			$grid->setDatas($result);
			$output = $grid->Display();
		}
		catch (Exception $e)
		{
			Fox\Message::Error($e->getMessage());
		}
	}

}
if(!$ajax)
{
	echo $screen->Display();
	echo '<div id="searchResult">'.$output.'</div>';
}
else
{
	echo Xview::instance()->messages();
	echo $output;
}